Skip to content

Conversation

@xterao
Copy link
Collaborator

@xterao xterao commented Jul 25, 2025

This update enables formatting for SQL code injected via @Sql annotations.


Implementation Details

  • A custom formatting model for the SQL language is applied individually to SQL written inside @Sql annotations.
  • After the Java file itself is formatted, a PostProcessor re-formats the SQL segments extracted from PsiLiteralExpression elements.

Note

In some cases, formatting may not be applied cleanly if:

  • The lines inside a text block are not consistently aligned to the left
  • Leading whitespace is inconsistent across lines

Before

@Dao
public interface InjectionDao {
	/**
	 * Aligned to the left
	 * @return
	 */
	@Select
	@Sql("""
SELECT * from tableName where id = 0""")
	Emp selectInjection();

	/**
	 * The tops are aligned
	 * @param id
	 * @return
	 */
	@Select
	@Sql("""
    SELECT *  from tableName where id = 
    /* id */1""")
	Emp selectInjection2(Integer id);

	/**
	 * Uneven lines
	 * @param id
	 * @return
	 */
	@Select
	@Sql("""
       /** TopBlock */
    select Count(distinct (x))
       , o.* , log(nbor.nearest
         , 999) AS nearest -- column Line commen
  /** From */ from user""")
	Emp selectInjection3(Integer id);
}

Formatted

@Dao
public interface InjectionDao {
	/**
	 * Aligned to the left
	 *
	 * @return
	 */
	@Select
	@Sql("""
SELECT *
  FROM tableName
 WHERE id = 0""")
	Emp selectInjection();

	/**
	 * The tops are aligned
	 *
	 * @param id
	 * @return
	 */
	@Select
	@Sql("""
    SELECT *
      FROM tableName
     WHERE id = /* id */1""")
	Emp selectInjection2(Integer id);

	/**
	 * Uneven lines
	 *
	 * @param id
	 * @return
	 */
	@Select
	@Sql("""
/** TopBlock */
  SELECT Count(DISTINCT (x))
         , o.*
       , log(nbor.nearest
               , 999) AS nearest -- column Line commen
        /** From */
  FROM user""")
	Emp selectInjection3(Integer id);
}

@xterao xterao self-assigned this Jul 25, 2025
@xterao xterao linked an issue Jul 25, 2025 that may be closed by this pull request
@xterao xterao merged commit 66bed16 into feature/sql-format-official-version Jul 25, 2025
5 checks passed
@xterao xterao deleted the feature/sql-format-support-injectopn-sql branch July 25, 2025 09:44
@xterao xterao restored the feature/sql-format-support-injectopn-sql branch July 28, 2025 00:32
@xterao xterao deleted the feature/sql-format-support-injectopn-sql branch August 4, 2025 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

【Formatter】Support SQL Format in @Sql

2 participants